#include "playlistdisplay.h"
#include "ui_playlistdisplay.h"
#include "QLabel"
#include "button.h"

bool sortByName2(Button * button1, Button * button2) {
    return button1->text().toLower()<button2->text().toLower();
}

PlaylistDisplay::PlaylistDisplay(QWidget *parent, FileSelector * fs) :
    KeyControllableMenu(parent),
    ui(new Ui::PlaylistDisplay)
{
    WindowManager::addWidget(this);
    QString unSelectedStyleSheet="background: rgba(255, 255, 255, 15); border: 1px solid rgba(255, 255, 255, 0); text-align: left; color: white;";
    QString selectedStyleSheet="background: qlineargradient(spread:pad, x1:0.07, y1:0.153409, x2:1, y2:1, stop:0 rgba(0, 0, 0, 50), stop:1 rgba(255, 255, 255, 70)); color: white; text-align: left; border: 1px solid rgba(255, 255, 255, 200);";
    QString inactiveSelectedStyleSheet="background: qlineargradient(spread:pad, x1:0.07, y1:0.153409, x2:1, y2:1, stop:0 rgba(255, 255, 255, 100), stop:1 rgba(255, 255, 255, 200)); color: white; text-align: left; border: 1px solid rgba(255, 255, 255, 200)";
    setStyleSheets(selectedStyleSheet,unSelectedStyleSheet, inactiveSelectedStyleSheet);
    this->fs = fs;
    QList <Button *> playlistNames;
    QSqlQueryModel * model= Querier::getModel("SELECT DISTINCT playlistName FROM Playlists");
    for(int i = 0; i < model->rowCount(); i++){
        QString playlist =  model->record(i).value("playlistName").toString();
        playlistNames.append(new Button (QIcon(""),playlist,this,new QLabel(model->record(i).value("playlistName").toString())));
    }
    qSort(playlistNames.begin(),playlistNames.end(),sortByName2);
    setLayout(new QVBoxLayout());
    foreach(Button * playlist, playlistNames){
        layout()->addWidget(playlist);
        addRow(playlist);
    }

    rightclick=new PopupMenu(this);
    connect(rightclick,SIGNAL(selected(Button*)),this,SLOT(buttonSelected(Button*)));

    removePlaylistButton=new Button(QIcon(""),"Delete playlist",rightclick);
    rightclick->addButton(removePlaylistButton);

    cancelButton=new Button(QIcon(""),"Cancel",rightclick);
    rightclick->addButton(cancelButton);

    rightclick->hide();
    ui->setupUi(this);

}

void PlaylistDisplay::buttonSelected(){
    QLabel * label = qobject_cast<QLabel *>(getCurrentButton()->getItem());
    qDebug()<<"pl.folderID = '"+ label->text() + "'";
    fs->setFilter("mf.mediaID IN (SELECT mediaID FROM playlists WHERE playlistName='"+ label->text()+"')");
    fs->reloadFiles(label->text());
    WindowManager::showWidget(fs);
}

void PlaylistDisplay::buttonSelected(Button *button) {
    if(button == removePlaylistButton)
    {
        rightclick->deactivate();
        QLabel * label = qobject_cast<QLabel *>(getCurrentButton()->getItem());
        QString playlistName = label->text();
        Querier::getResult("DELETE FROM Playlists WHERE playlistName = '" + playlistName +"'");
        this->reloadPlaylists();
        activate();
    } else {
        rightclick->deactivate();
        activate();
    }
}

void PlaylistDisplay::buttonRightClicked() {
    displayPopup(rightclick,
                 30+frameGeometry().x()+getCurrentButton()->frameGeometry().x()+getCurrentButton()->width(),
                 getCurrentButton()->y()+50);
    rightclick->activate();
}

void PlaylistDisplay::reshowComponents(){
    //reloadPlaylists();
}

void PlaylistDisplay::reloadPlaylists(){
    foreach( QObject * object, children() ) {
         Button * button=qobject_cast<Button *>(object);
         if(button) {
             removeButton(button);
             delete button;
         }
    }
    QList <Button *> playlistNames;
    QSqlQueryModel * model= Querier::getModel("SELECT DISTINCT playlistName FROM Playlists");
    for(int i = 0; i < model->rowCount(); i++){
        QString playlist =  model->record(i).value("playlistName").toString();
        playlistNames.append(new Button (QIcon(":/imgs/Playlist.png"),playlist,this,new QLabel(model->record(i).value("playlistName").toString())));
    }
    qSort(playlistNames.begin(),playlistNames.end(),sortByName2);
    setLayout(new QVBoxLayout());
    foreach(Button * playlist, playlistNames){
        layout()->addWidget(playlist);
        layout()->setAlignment(playlist,Qt::AlignTop);

        playlist->setIconSize(QSize(64,64));
        playlist->setMaximumWidth(QApplication::desktop()->screen(0)->width()/2);
        playlist->setMinimumWidth(QApplication::desktop()->screen(0)->width()/2);
        playlist->setMinimumHeight(70);
        addRow(playlist);
    }
    ui->setupUi(this);
}

PlaylistDisplay::~PlaylistDisplay()
{
    delete ui;
}

void PlaylistDisplay::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
